﻿using SuperX.Common.AppConfiguration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace SuperX.Common.Log
{
    /// <summary>
    /// 日志
    /// </summary>
    public static class Logger
    {
        // 
        static Logger()
        {
            string path;
            if (SuperX.Common.Helper.RuntimeEnvironment.IsLinux())
            {
                path = "/IOTData/logs";
            }
            else
            {
                path = SystemCfgService.GetInstance().SystemCfg.LogConfig.LocalLogPath;
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            Logger.Log.InitNLog(AppDomain.CurrentDomain.FriendlyName, path);
        }

        // 
        public static void Trace(string content)
        {
            Logger.Log.Trace(content);
        }

        // 
        public static void Debug(string content)
        {
            Logger.Log.Debug(content);
        }

        // 
        public static void Info(string content)
        {
            Logger.Log.Info(content);
        }

        // 
        public static void Warn(string content)
        {
            Logger.Log.Warn(content);
        }

        //
        public static void Error(string content)
        {
            Logger.Log.Error(content);
        }

        // 
        public static void Exception(Exception exception)
        {
            if (exception == null)
            {
                return;
            }
            Logger.Log.Error(exception.ToString());
        }

        // 
        public static void Fatal(string content)
        {
            Logger.Log.Fatal(content);
        }

        // 
        public static void ErrorException(Exception exception)
        {
            if (exception == null)
            {
                return;
            }
            Logger.Log.Error(exception.ToString());
        }

        // 
        public static void WarnException(Exception exception)
        {
            if (exception == null)
            {
                return;
            }
            Logger.Log.Warn(exception.ToString());
        }

        // 
        public static void TraceMessage(string message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log.Trace($"{message}|{memberName}|{sourceFilePath}|{sourceLineNumber}");
        }

        public static LoggerBase Log = new LoggerBase();
    }
}
